package cn.sxt.clinic.controller;

import cn.sxt.clinic.pojo.Stock;
import cn.sxt.clinic.service.IStockService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-07-01
 */
@Controller
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private IStockService stockService;

    @RequestMapping("/stockManager")
    public String stockManager(){
        return "stockManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword){
        QueryWrapper<Stock> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("medicine_name",keyword);
            wrapper.or();
            wrapper.like("dictionary_type",keyword);
        }
        IPage<Stock> page=new Page<>(pageNum,pageSize);
        IPage<Stock> result = stockService.page(page, wrapper);
        HashMap<String , Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return map;
    }

}

